package com.biz.primus.management.commodity.controller;

import com.biz.primus.commodity.vo.CategoryQueryVo;
import com.biz.primus.commodity.vo.ExtendDetailVo;
import com.biz.primus.commodity.vo.ExtendQueryVo;
import com.biz.primus.commodity.vo.ExtendVo;
import com.biz.primus.commodity.vo.backend.CategoryListItemVo;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.commodity.feign.CategoryFeignClient;
import com.biz.primus.management.commodity.feign.ExtendFeignClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("management/extend")
public class ExtendController extends AbstractBaseController {

    private final CategoryFeignClient categoryFeignClient;

    private final ExtendFeignClient extendFeignClient;

    public ExtendController(CategoryFeignClient categoryFeignClient, ExtendFeignClient extendFeignClient) {
        this.categoryFeignClient = categoryFeignClient;
        this.extendFeignClient = extendFeignClient;
    }

    @GetMapping(value = "list")
    @ResponseBody
//    @PreAuthorize("hasAuthority('OPT_EXTEND_LIST')")
    public ModelAndView list(@ModelAttribute("reqVo") ExtendQueryVo reqVo) {
        SpringPageVO<ExtendDetailVo> extendPage = extendFeignClient.findAll(reqVo);
        SpringPageVO<CategoryListItemVo> categories = categoryFeignClient.findAll(new CategoryQueryVo());
        return new ModelAndView("extend/list", "pages", extendPage).addObject("categories",categories);
    }

    @PostMapping(value = "save")
//    @PreAuthorize("hasAnyAuthority('OPT_EXTEND_UPDATE')")
    public ModelAndView save(String id,ExtendVo extendVo){
        if (StringUtils.isNotBlank(id)) {
            extendFeignClient.update(id, extendVo);
        } else {
            extendFeignClient.create(extendVo);
        }
        return new ModelAndView("redirect:/management/extend/list");
    }

    @RequestMapping("{id}")
//    @PreAuthorize("hasAuthority('OPT_EXTEND_DETAIL')")
    public ModelAndView edit(@PathVariable Long id) {
        return new ModelAndView("extend/detail")
                .addObject("extend", extendFeignClient.findExtendById(id));
    }


}
